<script lang="ts" setup>
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type {
  KnowledgeList,
  KnowledgeParams,
  KnowledgeType,
} from '@/types/consult'
import { getKnowledgePage } from '@/services/consult'

// 需要父组件接受的参数进行发送请求
const props = defineProps<{
  type: KnowledgeType
}>()

// 缓存列表数据
const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)

// 请求的参数
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10,
})
const onLoad = async () => {
  // 调用接口
  const res = await getKnowledgePage(params.value)
  list.value.push(...res.data.rows) // 异步更新数据

  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }
  loading.value = false
}
</script>

<template>
  <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
    class="knowledge-list"
  >
    <KnowledgeCard
      v-for="item in list"
      :key="item.id"
      :item="item"
    ></KnowledgeCard>
  </van-list>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
